
package com.hlkj.pay.app.merchant.ext.impl.heepay.handler;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.http.template.OkHttpTemplate;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantAccessAuthQueryContext;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantBusinessContext;
import com.hlkj.pay.app.merchant.ext.impl.heepay.dto.HeePayAccessExtentDto;
import com.hlkj.pay.app.merchant.ext.impl.heepay.dto.HeePayConfig;
import com.hlkj.pay.app.merchant.ext.impl.heepay.dto.HeePaySubInfo;
import com.hlkj.pay.app.merchant.ext.impl.heepay.dto.HeePayWechatConfig;
import com.hlkj.pay.dto.merchant.MerchantWechatConfigDto;
import com.hlkj.pay.dto.merchant.MerchantWechatConfigListDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessAuthDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.pay.PayChannelRequestDO;
import com.hlkj.pay.vo.order.req.MerchantChannelSplitReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author HlpayTeam
 * @date 2024/10/16 9:10
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Component
public class HeePayMerchantAccessConfigHandler extends HeePayMerchantCommonParamsHandle {

    private final OkHttpTemplate okHttpTemplate;

    /**
     * 微信参数配置查询
     *
     * @param channelMerchantBusinessContext
     */
    public void processSubMerInfoQuery(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<HeePayConfig> heePayConfigCommonResult = heePayConfig(channelMerchantBusinessContext.getProviderChannelConfig(), channelMerchantBusinessContext.isTest());
        // 配置转换
        if (!heePayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(heePayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        HeePayConfig heePayConfig = heePayConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        try {
            List<MerchantAccessAuthDO> merchantAccessAuthList = new ArrayList<>();
            HeePayAccessExtentDto heePayAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), HeePayAccessExtentDto.class);
            Map<String, Object> params = new HashMap<>();
            params.put("customer_id", heePayAccessExtentDto.getCustomerId());
            params.put("service_type", 1000);
            CommonResult<Map<String, String>> commonResult = commonRequest(params, "customer.channel-entry.query", heePayConfig, channelMerchantBusinessContext.isTest(), null);
            log.info("heePay 返回参数:{}", JsonUtils.toJsonString(commonResult));
            if (!commonResult.isSuccess()) {
                // channelMerchantBusinessContext.setProcessStatus(false);
                channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
                channelMerchantBusinessContext.setRemark(commonResult.getMsg());
                return;
            }
            Map<String, String> resultData = commonResult.getData();
            String entryInfo = resultData.get("entry_info");
            List<HeePaySubInfo> heePaySubInfos = JsonUtils.parseArray(entryInfo, HeePaySubInfo.class);
            for (HeePaySubInfo heePaySubInfo : heePaySubInfos) {
                MerchantAccessAuthDO merchantAccessAuthDO = new MerchantAccessAuthDO();
                // 1=微信,2=支付宝,3=银联
                switch (heePaySubInfo.getEntry_channel()) {
                    case "1":
                        merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.WECHAT.getCode());
                        break;
                    case "2":
                        merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.ALIPAY.getCode());
                        break;
                    default:
                        continue;
                }
                // 进件状态，-1=进件失败,0=未知,1=进件成功
                if ("-1".equals(heePaySubInfo.getEntry_status())) {
                    merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.REJECT.getCode());
                }
                else if ("0".equals(heePaySubInfo.getEntry_status())) {
                    merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVEING.getCode());
                }
                else {
                    merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVE.getCode());
                }
                merchantAccessAuthDO.setRejectReason(heePaySubInfo.getFail_reason());
                merchantAccessAuthDO.setSubMchId(heePaySubInfo.getSub_mchid());
                merchantAccessAuthList.add(merchantAccessAuthDO);
            }
            channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_CONFIG);
            channelMerchantBusinessContext.setMerchantAccessAuthList(merchantAccessAuthList);
            channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("汇付宝商户报备信息查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantBusinessContext.setRemark("汇付宝商户报备信息查询请求失败,失败信息" + e.getMessage());
            // channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
            // channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    public void processWxConfigSetAdd(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<HeePayConfig> heePayConfigCommonResult = heePayConfig(channelMerchantBusinessContext.getProviderChannelConfig(), channelMerchantBusinessContext.isTest());
        // 配置转换
        if (!heePayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(heePayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        HeePayConfig heePayConfig = heePayConfigCommonResult.getData();
        // 1. 数据初始化
        PayChannelRequestDO payChannelRequestDO = new PayChannelRequestDO();
        channelMerchantBusinessContext.setPayChannelRequestDO(payChannelRequestDO);
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        MerchantWechatConfigDto merchantWechatConfigDto = channelMerchantBusinessContext.getMerchantWechatConfigDto();
        try {
            HeePayAccessExtentDto heePayAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), HeePayAccessExtentDto.class);
            if (StringUtils.hasText(merchantWechatConfigDto.getAppId())) {
                Map<String, Object> params = new HashMap<>();
                params.put("customer_id", heePayAccessExtentDto.getCustomerId());
                params.put("service_type", 1000);
                params.put("small_app_id", merchantWechatConfigDto.getAppId());
                CommonResult<Map<String, String>> commonResult = commonRequest(params, "wechat.small.app.id.bind", heePayConfig, channelMerchantBusinessContext.isTest(), null);
                if (!commonResult.isSuccess()) {
                    channelMerchantBusinessContext.setProcessStatus(false);
                    channelMerchantBusinessContext.setRemark(commonResult.getMsg());
                }
                else {
                    Map<String, String> resultData = commonResult.getData();
                    String auth_status = resultData.get("auth_status");
                    // if (!"1".equals(auth_status)) {
                    //     channelMerchantBusinessContext.setProcessStatus(false);
                    //     channelMerchantBusinessContext.setRemark(resultData.get("auth_desc"));
                    // }
                }
            }
            if (StringUtils.hasText(merchantWechatConfigDto.getJsAPIPath())) {
                Map<String, Object> params = new HashMap<>();
                params.put("customer_id", heePayAccessExtentDto.getCustomerId());
                params.put("service_type", 1000);
                params.put("auth_channel", 1);
                params.put("auth_pay_url", merchantWechatConfigDto.getJsAPIPath());
                CommonResult<Map<String, String>> commonResult = commonRequest(params, "customer.auth.catalog.add", heePayConfig, channelMerchantBusinessContext.isTest(), null);
                if (!commonResult.isSuccess()) {
                    channelMerchantBusinessContext.setProcessStatus(false);
                    channelMerchantBusinessContext.setRemark(commonResult.getMsg());
                }
                else {
                    Map<String, String> resultData = commonResult.getData();
                    String auth_status = resultData.get("auth_status");
                    // if (!"1".equals(auth_status)) {
                    //     channelMerchantBusinessContext.setProcessStatus(false);
                    //     channelMerchantBusinessContext.setRemark(resultData.get("auth_desc"));
                    // }
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("汇付宝微信配置add请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantBusinessContext.setRemark("汇付宝微信配置add请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 微信参数配置查询
     *
     * @param channelMerchantAccessAuthQueryContext
     */
    public void processWxConfigQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        CommonResult<HeePayConfig> heePayConfigCommonResult = heePayConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        // 配置转换
        if (!heePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(heePayConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        HeePayConfig heePayConfig = heePayConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        HeePayAccessExtentDto heePayAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), HeePayAccessExtentDto.class);
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("customer_id", heePayAccessExtentDto.getCustomerId());
            params.put("auth_channel", 1);
            CommonResult<Map<String, String>> commonResult = commonRequest(params, "customer.auth.catalog.get", heePayConfig, channelMerchantAccessAuthQueryContext.isTest(), null);
            if (!commonResult.isSuccess()) {
                channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                channelMerchantAccessAuthQueryContext.setRemark(commonResult.getMsg());
            }

            Map<String, String> resultData = commonResult.getData();
            String authPayUrlList = resultData.get("auth_pay_url_list");
            if (!StringUtils.hasText(authPayUrlList)) {
                return;
            }
            List<HeePayWechatConfig> heePayWechatConfigList = JsonUtils.parseArray(authPayUrlList, HeePayWechatConfig.class);
            MerchantWechatConfigListDto merchantWechatConfigListDto = new MerchantWechatConfigListDto();
            List<String> jsApiPath = heePayWechatConfigList.stream().map(heePayWechatConfig -> heePayWechatConfig.getAuth_pay_url()).collect(Collectors.toList());
            merchantWechatConfigListDto.setJsApiPath(jsApiPath);
            merchantWechatConfigListDto.setJsApiPath(jsApiPath);
            channelMerchantAccessAuthQueryContext.setMerchantWechatConfigListDto(merchantWechatConfigListDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("汇付宝微信配置查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessAuthQueryContext.setRemark("汇付宝微信配置查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessAuthQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 微信参数配置查询
     *
     * @param channelMerchantAccessAuthQueryContext
     */
    public void processWxAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        CommonResult<HeePayConfig> heePayConfigCommonResult = heePayConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        // 配置转换
        if (!heePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(heePayConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        HeePayConfig heePayConfig = heePayConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        try {
            Map<String, Object> params = new HashMap<>();
            HeePayAccessExtentDto heePayAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), HeePayAccessExtentDto.class);
            params.put("customer_id", heePayAccessExtentDto.getCustomerId());
            params.put("service_type", 1000);
            List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantAccessAuthQueryContext.getMerchantAccessAuthDbList();
            channelMerchantAccessAuthQueryContext.setMerchantAccessAuthList(merchantAccessAuthList);
            // 没有微信报备 执行支付宝认证
            if (CollectionUtils.isEmpty(merchantAccessAuthList)) {
                channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
                return;
            }
            MerchantAccessAuthDO merchantAccessAuthDO = merchantAccessAuthList.get(0);
            CommonResult<Map<String, String>> commonResult;
            if (!StringUtils.hasText(merchantAccessAuthDO.getSubMchId())) {
                commonResult = commonRequest(params, "customer.channel-entry.query", heePayConfig, channelMerchantAccessAuthQueryContext.isTest(), null);
                if (commonResult.isSuccess()) {
                    Map<String, String> channelResultData = commonResult.getData();
                    String entryInfo = channelResultData.get("entry_info");
                    List<HeePaySubInfo> heePaySubInfos = JsonUtils.parseArray(entryInfo, HeePaySubInfo.class);
                    for (HeePaySubInfo heePaySubInfo : heePaySubInfos) {
                        // 1=微信,2=支付宝,3=银联
                        switch (heePaySubInfo.getEntry_channel()) {
                            case "1":
                                merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.ALIPAY.getCode());
                                break;
                            default:
                                continue;
                        }
                        merchantAccessAuthDO.setSubMchId(heePaySubInfo.getSub_mchid());
                        merchantAccessAuthDO.setRejectReason(heePaySubInfo.getFail_reason());
                        // merchantAccessAuthList.add(merchantAccessAuthDO);
                    }
                }
            }
            commonResult = commonRequest(params, "customer.wx.query", heePayConfig, channelMerchantAccessAuthQueryContext.isTest(), null);
            if (!commonResult.isSuccess()) {
                channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                channelMerchantAccessAuthQueryContext.setRemark(commonResult.getMsg());
                return;
            }

            Map<String, String> resultData = commonResult.getData();
            String applyment_state = resultData.get("applyment_state");
            String qrcode_data = resultData.get("qrcode_data");
            String authorize_state = resultData.get("authorize_state");
            if (StringUtils.hasText(qrcode_data)) {
                byte[] imageBytes = Base64.getDecoder().decode(qrcode_data);
                merchantAccessAuthDO.setQrCodeArray(imageBytes);
            }
            if (StringUtils.hasText(applyment_state)) {
                switch (applyment_state) {
                    case "APPLYMENT_STATE_FAIL":
                    case "APPLYMENT_STATE_REJECTED":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.REJECT.getCode());
                        break;
                    // case "APPLYMENT_STATE_COMMIT":
                    case "APPLYMENT_STATE_WAITTING_FOR_AUDIT":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVEING.getCode());
                        break;
                    case "APPLYMENT_STATE_EDITTING":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.NO_APPLY.getCode());
                        break;
                    case "APPLYMENT_STATE_WAITTING_FOR_CONFIRM_CONTACT":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_CONFIRM.getCode());
                        break;
                    case "APPLYMENT_STATE_WAITTING_FOR_CONFIRM_LEGALPERSON":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_AUTH.getCode());
                        break;
                    case "APPLYMENT_STATE_PASSED":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVE.getCode());
                        break;
                    case "APPLYMENT_STATE_FREEZED":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.FROZEN.getCode());
                        break;
                    case "APPLYMENT_STATE_CANCELED":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.CANCEL.getCode());
                        break;
                    default:
                }
            }
            if (StringUtils.hasText(authorize_state)) {
                switch (authorize_state) {
                    case "AUTHORIZE_STATE_AUTHORIZED":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.AUTH_SUCCESS.getCode());
                        break;
                    default:
                }
            }

        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("汇付宝微信认证查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessAuthQueryContext.setRemark("汇付宝微信认证查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessAuthQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    /**
     * 微信参数配置查询
     *
     * @param channelMerchantAccessAuthQueryContext
     */
    public void processAliAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        CommonResult<HeePayConfig> heePayConfigCommonResult = heePayConfig(channelMerchantAccessAuthQueryContext.getProviderChannelConfig(),
                channelMerchantAccessAuthQueryContext.isTest());
        // 配置转换
        if (!heePayConfigCommonResult.isSuccess()) {
            channelMerchantAccessAuthQueryContext.setRemark(heePayConfigCommonResult.getMsg());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        HeePayConfig heePayConfig = heePayConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        try {
            Map<String, Object> params = new HashMap<>();
            HeePayAccessExtentDto heePayAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), HeePayAccessExtentDto.class);
            params.put("customer_id", heePayAccessExtentDto.getCustomerId());
            params.put("service_type", 1000);
            List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantAccessAuthQueryContext.getMerchantAccessAuthDbList();
            channelMerchantAccessAuthQueryContext.setMerchantAccessAuthList(merchantAccessAuthList);
            // 没有微信报备 执行支付宝认证
            if (CollectionUtils.isEmpty(merchantAccessAuthList)) {
                channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
                return;
            }
            MerchantAccessAuthDO merchantAccessAuthDO = merchantAccessAuthList.get(0);
            CommonResult<Map<String, String>> commonResult;
            if (!StringUtils.hasText(merchantAccessAuthDO.getSubMchId())) {
                commonResult = commonRequest(params, "customer.channel-entry.query", heePayConfig, channelMerchantAccessAuthQueryContext.isTest(), null);
                if (commonResult.isSuccess()) {
                    Map<String, String> channelResultData = commonResult.getData();
                    String entryInfo = channelResultData.get("entry_info");
                    List<HeePaySubInfo> heePaySubInfos = JsonUtils.parseArray(entryInfo, HeePaySubInfo.class);
                    for (HeePaySubInfo heePaySubInfo : heePaySubInfos) {
                        // 1=微信,2=支付宝,3=银联
                        switch (heePaySubInfo.getEntry_channel()) {
                            case "2":
                                merchantAccessAuthDO.setType(PayEnums.PAY_TYPE.WECHAT.getCode());
                                break;
                            default:
                                continue;
                        }
                        merchantAccessAuthDO.setSubMchId(heePaySubInfo.getSub_mchid());
                        merchantAccessAuthDO.setRejectReason(heePaySubInfo.getFail_reason());
                        // merchantAccessAuthList.add(merchantAccessAuthDO);
                    }
                }
            }
            commonResult = commonRequest(params, "customer.alipay.query", heePayConfig, channelMerchantAccessAuthQueryContext.isTest(), null);
            if (!commonResult.isSuccess()) {
                channelMerchantAccessAuthQueryContext.setProcessStatus(false);
                channelMerchantAccessAuthQueryContext.setRemark(commonResult.getMsg());
                return;
            }

            Map<String, String> resultData = commonResult.getData();
            String applyment_state = resultData.get("applyment_state");
            String qrcode_data = resultData.get("qrcode_data");
            String authorize_state = resultData.get("authorize_state");
            if (StringUtils.hasText(qrcode_data)) {
                merchantAccessAuthDO.setAuthUrl(qrcode_data);
            }
            if (StringUtils.hasText(applyment_state)) {
                switch (applyment_state) {
                    case "AUDIT_REJECT":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.REJECT.getCode());
                        break;
                    case "AUDITING":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVEING.getCode());
                        break;
                    case "CONTACT_CONFIRM":
                    case "CONTACT_PROCESSING":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_CONFIRM.getCode());
                        break;
                    case "LEGAL_CONFIRM":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.WAIT_AUTH.getCode());
                        break;
                    case "AUDIT_PASS":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVE.getCode());
                        break;
                    case "AUDIT_FREEZE":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.FROZEN.getCode());
                        break;
                    case "CANCELED":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.CANCEL.getCode());
                        break;
                    default:
                }
            }
            if (StringUtils.hasText(authorize_state)) {
                switch (authorize_state) {
                    case "AUTHORIZED":
                        merchantAccessAuthDO.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.AUTH_SUCCESS.getCode());
                        break;
                    default:
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("汇付宝支付宝认证查询请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantAccessAuthQueryContext.setRemark("汇付宝支付宝认证查询请求失败,失败信息" + e.getMessage());
            channelMerchantAccessAuthQueryContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantAccessAuthQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }

    public void channelSplit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<HeePayConfig> heePayConfigCommonResult = heePayConfig(channelMerchantBusinessContext.getProviderChannelConfig(), channelMerchantBusinessContext.isTest());
        // 配置转换
        if (!heePayConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(heePayConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        HeePayConfig heePayConfig = heePayConfigCommonResult.getData();
        // 1. 数据初始化
        MerchantChannelSplitReq merchantChannelSplitReq = channelMerchantBusinessContext.getMerchantChannelSplitReq();
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo();
        try {
            Map<String, Object> params = new HashMap<>();
            HeePayAccessExtentDto heePayAccessExtentDto = JsonUtils.parseObject(merchantAccessInfo.getExtendA(), HeePayAccessExtentDto.class);
            params.put("customer_id", heePayAccessExtentDto.getCustomerId());
            params.put("service_type", 1000);
            params.put("channel_code", merchantChannelSplitReq.getChannelNo());
            MerchantAccessAuthDO merchantAccessAuth=new MerchantAccessAuthDO();
            merchantAccessAuth.setMchId(merchantChannelSplitReq.getChannelNo());
            if ("ALIPAY".equals(merchantChannelSplitReq.getTradeType())) {
                params.put("entry_channel", 2);
                merchantAccessAuth.setType(PayEnums.PAY_TYPE.ALIPAY.getCode());
            }
            else if ("WECHAT".equals(merchantChannelSplitReq.getTradeType())) {
                params.put("entry_channel", 1);
                merchantAccessAuth.setType(PayEnums.PAY_TYPE.WECHAT.getCode());
            }
            else {
                channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantBusinessContext.setRemark("汇付宝微信/支付宝拆分失败：当前拆分类型不支持 提交的tradeType为:" + merchantChannelSplitReq.getTradeType());
                return;
            }
            CommonResult<Map<String, String>> commonResult = commonRequest(params, "channel.entry.apply", heePayConfig, channelMerchantBusinessContext.isTest(), null);
            merchantAccessAuth.setAccessId(merchantAccessInfo.getId());
            merchantAccessAuth.setSn(merchantAccessInfo.getSn());
            channelMerchantBusinessContext.setMerchantAccessAuthDO(merchantAccessAuth);
            if (!commonResult.isSuccess()) {
                channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
                channelMerchantBusinessContext.setRemark("汇付宝微信/支付宝拆分失败：" + commonResult.getMsg());
                merchantAccessAuth.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.AUTH_FAIL.getCode());
                merchantAccessAuth.setState(CommonEnum.YES_NO_TYPE.NO.getCode());
                merchantAccessAuth.setRejectReason(commonResult.getMsg());
                log.error("汇付宝微信/支付宝拆分失败 message: {}", commonResult.getMsg());
            }else {
                Map<String, String> resultData = commonResult.getData();
                String bankId = resultData.get("bank_id");
                String subMchid = resultData.get("sub_mchid");
                merchantAccessAuth.setSerialCode(bankId);
                merchantAccessAuth.setSubMchId(subMchid);
                merchantAccessAuth.setStatus(MerchantEnum.MERCHANT_AUTH_STATUS.APPROVEING.getCode());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("汇付宝微信/支付宝拆分请求失败,失败信息 message: {}", e.getMessage());
            channelMerchantBusinessContext.setRemark("汇付宝微信/支付宝拆分请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            // channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_REFUSE);
        }
    }
}
